package com.mf.all.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mf.all.entity.dto.TaskBo;
import com.mf.all.entity.req.TaskReq;
import com.mf.all.entity.req.publicReq.PublicUpdateStatusReq;
import com.mf.all.entity.req.search.TaskSearchReq;
import com.mf.all.service.ITaskNotificationService;
import com.mf.all.service.ITaskService;
import com.mf.all.exception.GraceException;
import com.mf.all.groups.AddGroupsInterface;
import com.mf.all.groups.UpdateGroupsInterface;
import com.mf.all.result.Result;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.springframework.beans.BeanUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.util.List;


/**
 * 任务表
 *
 * @author mf
 * @since 2025-03-08
 */
@RestController
@RequestMapping("task/v1")
@Tag(name = "任务表")
public class TaskController {
    @Resource
    private ITaskService taskService;

    @Resource
    private ITaskNotificationService taskNotificationService;

    @Operation(summary = "更新指定日期任务通知")
    @GetMapping("updateTaskDayNotification/{date}")
    public Result<Void> updateTaskDayNotification(@PathVariable("date") String date) {
        // 字符串日期转为LocalDate类型
        taskService.updateTaskDayNotification(LocalDate.parse(date));
        return Result.success(null);
    }


    @Operation(summary = "新增任务")
    @PostMapping("save")
    public Result<Integer> save(@Validated(AddGroupsInterface.class) @RequestBody TaskReq taskReq) {
        return Result.success(taskService.saveTask(setTaskBo(taskReq)));
    }

    @Operation(summary = "编辑任务")
    @PostMapping("update")
    public Result<Integer> update(@Validated(UpdateGroupsInterface.class) @RequestBody TaskReq taskReq) {
        return Result.success(taskService.updateTaskById(setTaskBo(taskReq)));
    }

    @Operation(summary = "删除任务")
    @GetMapping("delete/{id}")
    public Result<Integer> delete(@PathVariable("id") int id) {
        taskService.verifyTaskStatus(id);
        return Result.success(taskService.deleteById(id));
    }

    @Operation(summary = "作废任务")
    @GetMapping("cancellation/{id}")
    public Result<Integer> cancellation(@PathVariable("id") int id) {
        taskService.verifyTaskStatus(id);
        return Result.success(taskService.cancellation(id));
    }

    @Operation(summary = "修改任务状态")
    @PostMapping("updateStatus")
    public Result<Integer> updateStatus(@Validated @RequestBody PublicUpdateStatusReq<Integer> publicUpdateStatusRequest) {
        return Result.success(taskService.updateStatusByIds(publicUpdateStatusRequest));
    }

    @Operation(summary = "分页查询任务")
    @PostMapping("findPageList")
    public Result<Page<TaskBo>> findPageList(@RequestBody TaskSearchReq taskSearchReq) {
        return Result.success(taskService.findPageList(taskSearchReq));
    }

    /**
     * 不分页查询任务(用于查询子任务)
     */
    @PostMapping("findList")
    public Result<List<TaskBo>> findList(@RequestBody TaskSearchReq taskSearchReq) {
        if (taskSearchReq.getPid() == null || taskSearchReq.getPid() < 1) {
            GraceException.display("pid不能小于0");
        }
        return Result.success(taskService.findByPidList(taskSearchReq));
    }


    private TaskBo setTaskBo(TaskReq taskReq) {
        TaskBo taskBo = new TaskBo();
        BeanUtils.copyProperties(taskReq, taskBo);
        return taskBo;
    }

}
